package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameInstanceSerializer;
import com.turnengine.client.global.game.bean.IGameInstance;
import java.io.IOException;

/**
 * The New Game Instance Data Serializer.
 */
public class NewGameInstanceDataSerializer extends AbstractCommandDataSerializer<IGameInstance> implements INewGameInstanceDataSerializer {

	/**
	 * Creates a new New Game Instance Data Serializer.
	 */
	public NewGameInstanceDataSerializer() {
		super(NewGameInstance.COMMAND_ID);
	}

	@Override
	public IGameInstance readReturnValue(IDataReader reader) throws IOException {
		IGameInstance value = new GameInstanceSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameInstance value) throws IOException {
		new GameInstanceSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameInstance> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		String gameName = reader.readString(true);
		String versionName = reader.readString(true);
		String instanceName = reader.readString(true);
		int hostId = reader.readInt();

		// Create object
		NewGameInstance object = new NewGameInstance();
		object.setLoginId(loginId);
		object.setGameName(gameName);
		object.setVersionName(versionName);
		object.setInstanceName(instanceName);
		object.setHostId(hostId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameInstance> argument) throws IOException {
		NewGameInstance object = (NewGameInstance) argument;

		// Get fields
		long loginId = object.getLoginId();
		String gameName = object.getGameName();
		String versionName = object.getVersionName();
		String instanceName = object.getInstanceName();
		int hostId = object.getHostId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeString(gameName, true);
		writer.writeString(versionName, true);
		writer.writeString(instanceName, true);
		writer.writeInt(hostId);
	}

	@Override
	public IExecutableCommandResponse<IGameInstance> newResponse() {
		return new NewGameInstanceResponse();
	}
}
